﻿namespace MicroCloud.Utils
{
    /// <summary>
    /// 枚举 <see cref="Enum"/> 的扩展辅助操作方法
    /// </summary>
    public static class EnumExtensions
    {
        /// <summary>
        /// 获取枚举项上的 <see cref="DescriptionAttribute"/> 特性的文字描述
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToDescription(this Enum value)
        {
            Type type = value.GetType();
            MemberInfo member = type.GetMember(value.ToString()).FirstOrDefault();
            return member != null ? member.GetDescription() : value.ToString();
        }

        /// <summary>
        /// 获取枚举项上的指定Attribute特性
        /// </summary>
        /// <typeparam name="T">Attribute特性类型</typeparam>
        /// <param name="value">枚举值</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>存在返回第一个，不存在返回null</returns>
        public static T GetAttribute<T>(this Enum value, bool inherit = true) where T : Attribute
        {
            Type type = value.GetType();
            MemberInfo member = type.GetMember(value.ToString()).FirstOrDefault();
            return member != null ? member.GetAttribute<T>(inherit) : default;
        }

        /// <summary>
        /// 获取枚举项上的指定Attribute特性
        /// </summary>
        /// <typeparam name="T">Attribute特性类型</typeparam>
        /// <param name="value">枚举值</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>返回所有指定Attribute特性的数组</returns>
        public static T[] GetAttributes<T>(this Enum value, bool inherit = true) where T : Attribute
        {
            Type type = value.GetType();
            MemberInfo member = type.GetMember(value.ToString()).FirstOrDefault();
            return member != null ? member.GetAttributes<T>(inherit) : Array.Empty<T>();
        }

        /// <summary>
        /// 转为 <see cref="IDictionary"/> 集合
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IDictionary<string, string> ToDictionary(this Enum value)
        {
            Type type = value.GetType();
            IDictionary<string, string> dict = new Dictionary<string, string>();

            foreach (var e in Enum.GetValues(type))
            {
                var enumDesction = e.GetType().GetField(e.ToString()).GetDescription(true);
                enumDesction = enumDesction.IsMissing() ? e.ToString() : enumDesction;

                dict.Add(e.ToString(), enumDesction);
            }
            return dict;
        }

        /// <summary>
        /// 转为 <see cref="EnumEntity"/> 集合
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static List<EnumEntity> ToList(this Enum value)
        {
            Type type = value.GetType();
            List<EnumEntity> list = new();

            foreach (var e in Enum.GetValues(type))
            {
                var enumDesction = e.GetType().GetField(e.ToString()).GetDescription(true);
                list.Add(new EnumEntity
                {
                    Value = Convert.ToInt32(e),
                    Name = e.ToString(),
                    Desc = enumDesction,
                });
            }
            return list;
        }

    }
}